package com.hctg.forced2sleep.easy_window;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.accessibility.AccessibilityNodeInfo;

import com.actor.myandroidframework.utils.ConfigUtils;
import com.actor.myandroidframework.utils.TextUtils2;
import com.actor.myandroidframework.utils.toaster.ToasterUtils;
import com.blankj.utilcode.util.AppUtils;
import com.hctg.forced2sleep.R;
import com.hctg.forced2sleep.bean.WeekBean;
import com.hctg.forced2sleep.databinding.EasyWindowMarkElementBinding;

/**
 * description: 编辑标记的元素信息
 * company    :
 *
 * @author : ldf
 * date       : 2024/10/27 on 18
 * @version 1.0
 */
public class MarkElementEasyWindow extends EasyWindow2<MarkElementEasyWindow> implements View.OnClickListener {

    private final EasyWindowMarkElementBinding viewBinding;
//    private String pageClass = "";
    private String path = "";
    private int startHour = 0, startMinute = 0, endHour = 23, endMinute = 59;

    /**
     * @param pageName 页面名称: Activity/Dialog 名称
     */
    public MarkElementEasyWindow(AccessibilityNodeInfo nodeInfo, String pageName) {
        super(ConfigUtils.APPLICATION);
        viewBinding = EasyWindowMarkElementBinding.inflate(LayoutInflater.from(getContext()), (ViewGroup) getDecorView(), false);
        setContentView(viewBinding.getRoot());
        setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        setTag(this.getClass().getName());

        //页面名称
        CharSequence packageName = nodeInfo.getPackageName();
        String appName = null;
        if (!TextUtils.isEmpty(packageName)) {
            appName = AppUtils.getAppName(packageName.toString());
        }
        if (!TextUtils.isEmpty(appName)) {
            //页面名称
            viewBinding.etPageName.setText(appName + "-");
        }
        //页面class（标识）
//        viewBinding.tvPageClass.setText(getPageClass(nodeInfo));
        viewBinding.tvPageClass.setText(pageName);
        //元素ID
        viewBinding.tvWidgetId.setText(nodeInfo.getViewIdResourceName());
        //元素Class
        viewBinding.tvWidgetClass.setText(nodeInfo.getClassName());
        //元素文字
        viewBinding.etWidgetText.setText(nodeInfo.getText());
        //匹配元素描述
        viewBinding.tvWidgetDesc.setText(nodeInfo.getContentDescription());
        //匹配元素描述
        viewBinding.tvWidgetPath.setText(getElementPath(nodeInfo));


        //禁用时间段
        viewBinding.stvBanTime.setOnClickListener(this);

        //更多专业设置
        viewBinding.tvMore.setOnClickListener(this);

        //页面Class标识
        viewBinding.itemPageClass.setOnClickListener(this);
        //匹配元素ID
        viewBinding.itemWidgetId.setOnClickListener(this);
        //匹配元素Class
        viewBinding.itemWidgetClass.setOnClickListener(this);
        //匹配元素文字
        viewBinding.itemWidgetText.setOnClickListener(this);
        //匹配元素描述
        viewBinding.itemWidgetDesc.setOnClickListener(this);
        //匹配元素索引路径
        viewBinding.itemWidgetPath.setOnClickListener(this);

        //完全匹配
        viewBinding.tab1.setOnClickListener(this);
        //包含匹配
        viewBinding.tab2.setOnClickListener(this);

        viewBinding.tvCancel.setOnClickListener(this);
        viewBinding.tvSave.setOnClickListener(this);


        viewBinding.tvSunday.setSelected(true);
        viewBinding.tvMonday.setSelected(true);
        viewBinding.tvTuesday.setSelected(true);
        viewBinding.tvWednesday.setSelected(true);
        viewBinding.tvThursday.setSelected(true);
        viewBinding.tvFriday.setSelected(true);
        viewBinding.tvSaturday.setSelected(true);

        viewBinding.tab1.setSelected(true);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.stv_ban_time: //禁用时间段
                WeekBean weekBean0 = new WeekBean();
                weekBean0.isSundaySelect = viewBinding.tvSunday.isSelected();
                weekBean0.isMondaySelect = viewBinding.tvMonday.isSelected();
                weekBean0.isTuesdaySelect = viewBinding.tvTuesday.isSelected();
                weekBean0.isWednesdaySelect = viewBinding.tvWednesday.isSelected();
                weekBean0.isThursdaySelect = viewBinding.tvThursday.isSelected();
                weekBean0.isFridaySelect = viewBinding.tvFriday.isSelected();
                weekBean0.isSaturdaySelect = viewBinding.tvSaturday.isSelected();
                new HourMinuteSelectEasyWindow(startHour, startMinute, endHour, endMinute, weekBean0,
                        (startHour, startMinute, endHour, endMinute, weekBean) -> {
                            this.startHour = startHour;
                            this.startMinute = startMinute;
                            this.endHour = endHour;
                            this.endMinute = endMinute;

                            viewBinding.tvSunday.setSelected(weekBean.isSundaySelect);
                            viewBinding.tvMonday.setSelected(weekBean.isMondaySelect);
                            viewBinding.tvTuesday.setSelected(weekBean.isTuesdaySelect);
                            viewBinding.tvWednesday.setSelected(weekBean.isWednesdaySelect);
                            viewBinding.tvThursday.setSelected(weekBean.isThursdaySelect);
                            viewBinding.tvFriday.setSelected(weekBean.isFridaySelect);
                            viewBinding.tvSaturday.setSelected(weekBean.isSaturdaySelect);

                            String timeArea = TextUtils2.getStringFormat("%02d:%02d至%02d:%02d", startHour, startMinute, endHour, endMinute);
                            viewBinding.stvBanTime.setText(timeArea);
                        }
                ).show();
                break;
            case R.id.item_page_class:  //匹配页面Class标识
            case R.id.item_widget_id:   //匹配元素ID
            case R.id.item_widget_class://匹配元素Class
            case R.id.item_widget_desc: //匹配元素描述
            case R.id.item_widget_path: //匹配元素索引路径
                view.setSelected(!view.isSelected());
                break;
            case R.id.item_widget_text: //匹配元素文字
                boolean tab1Se = viewBinding.tab1.isSelected();
                boolean tab2Se = viewBinding.tab2.isSelected();
                view.setSelected(!view.isSelected());
                viewBinding.tab1.setSelected(tab1Se);
                viewBinding.tab2.setSelected(tab2Se);
                break;
            case R.id.tab_1:
                view.setSelected(true);
                viewBinding.tab2.setSelected(false);
                break;
            case R.id.tab_2:
                view.setSelected(true);
                viewBinding.tab1.setSelected(false);
                break;
            case R.id.tv_more:      //更多专业设置
                view.setSelected(!view.isSelected());
                viewBinding.llMore.setVisibility(view.isSelected() ? View.VISIBLE : View.GONE);
                break;
            case R.id.tv_cancel:
                cancel();
                break;
            case R.id.tv_save:
                // TODO: 2024/11/20 还未实现!
                ToasterUtils.warning("还未实现!");
                break;
            default:
                break;
        }
    }

    /**
     * @param nodeInfo
     * @return android.widget.FrameLayout
     */
    private String getPageClass(AccessibilityNodeInfo nodeInfo) {
        if (nodeInfo == null) return null;
        AccessibilityNodeInfo parent = nodeInfo.getParent();
        if (parent == null) {
            CharSequence className = nodeInfo.getClassName();
            if (className != null) return className.toString();
        }
        return getPageClass(parent);
    }

    /**
     * 获取节点路径
     * @param nodeInfo
     * @return
     */
    private String getElementPath(AccessibilityNodeInfo nodeInfo) {
        if (nodeInfo == null) return path;
        CharSequence className = nodeInfo.getClassName();
        if (!TextUtils.isEmpty(className)) {
            if (path.isEmpty()) {
                path = className.toString();
            } else {
                path = className + "," + path;
            }
        }
        return getElementPath(nodeInfo.getParent());
    }
}
